#!/bin/bash

echo "#$HSTA_BENCHMARK.tcl for power profiling"
echo "#Author: Min Li, Contact: mli46@wisc.edu"
echo -ne "#Generated Date: "
date
echo 

## paste the sw file and prob file
let "HSTA_SET_CNT=$1+7"

echo "#Input files for parsing are: "
echo "#"$HSTA_CONTROL_INPUT_FILE
echo "#"$HSTA_REGION_FILE
echo "#"$HSTA_CUR_SW_FILE
echo

echo "read_verilog $HSTA_BENCHMARK_DIR/$HSTA_BENCHMARK.v"
echo "current_design $HSTA_BENCHMARK"
echo "link"
echo "set power_analysis_mode \"averaged\""
echo

## note the default values
echo "set power_default_toggle_rate 0.3"
echo "set power_default_static_probability 0.4"
echo

## parsing the control input file
echo "#******************For S35932**********************"
echo "#*****Set the values of the control inputs*********"
## remove [] for some files
awk '{ gsub(/\[/, "_"); gsub(/\]/, "") } 1' $HSTA_CONTROL_INPUT_FILE > $HSTA_UNIQ_TMP_FILE
awk '{print "set_case_analysis", $2, $1}' $HSTA_UNIQ_TMP_FILE
echo

## changable
echo "#**************Wire Load models********************"
echo "set_wire_load_mode enclosed"
echo "set_wire_load_model -name \"TSMC64K_Lowk_Conservative\""
echo

## changable, assume single clock
echo "create_clock -name CLK -period 10 [get_ports blif_clk_net]"
echo "set_dont_touch_network CLK"
echo

## parsing the group info
echo "#********Grouping the gates to form regions********"
REGION_NUM=$(awk 'NR==1 {print $1}' $HSTA_REGION_FILE)
#echo $REGION_NUM
echo

## there can be region numbers that are discontinued
line_cnt=2
for (( c=1; c<=$REGION_NUM; c++ ))
do
    temp=$(awk -v lcnt=$line_cnt 'NR==lcnt {print $1}' $HSTA_REGION_FILE)
    let line_cnt+=1
    if [ "$temp" == "#end" ] 
    then
        break
    fi
    reg_id=${temp:6}
    echo -ne "create_power_group -name region$reg_id [get_cells {"
    while [ 1 ]; do
        tmp=$(awk -v lcnt=$line_cnt 'NR==lcnt {print $1}' $HSTA_REGION_FILE)
        #echo $tmp
        let line_cnt+=1
        if [ "$tmp" == "!" ] 
        then
            echo "}]"
            break
        else
            echo -ne $tmp" "
        fi
    done
done
echo


## parsing the switching activity info
echo "#**********Set SW for certain signals**************"
#awk -v set_cnt=$HSTA_SET_CNT '{print "set_switching_activity -static_probability", \
#$set_cnt, "-state_condition \"default\" [get_net ", $1, "]" }' $HSTA_SW_FILE
paste $HSTA_PROB_FILE $HSTA_SW_FILE_GOLD | awk -v set_cnt=$HSTA_SET_CNT '{print "set_switching_activity -static_probability " $4 " -toggle_rate", $set_cnt, "-state_condition \"default\" [get_net ", $5, "]" }'
echo

## power_report
echo "#***************Output power report****************"
#echo "report_power_groups"
echo "report_power -verbose"
echo "exit"
echo
